import 'package:easy_refresh/easy_refresh.dart';
import 'package:f200/common/index.dart';
import 'package:f200/features/categroy/application/controller/category_screen_controller.dart';
import 'package:f200/features/goods/data/goods_repository.dart';
import 'package:f200/features/goods/presentation/widgets/goods_item_widget.dart';
import 'package:f200/routes/routes.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../data/category_repository.dart';
import 'widgets/classify_desc.dart';

class CategoryScreen extends StatefulWidget {
  const CategoryScreen({super.key});

  @override
  State<CategoryScreen> createState() => _CategoryScreenState();
}

class _CategoryScreenState extends State<CategoryScreen>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return CategoryScreenX();
  }

  @override
  bool get wantKeepAlive => true;
}

class CategoryScreenX extends GetView<CategoryScreenController> {
  const CategoryScreenX({super.key});

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          title: const Text('分类'),
          elevation: 0,
        ),
        body: Container(
          child: Row(
            children: <Widget>[
              _leftView(),
              _rightView(),
            ],
          ),
        ),
      ),
    );
  }

  ///右侧视图
  Widget _rightView() {
    return Container(
      width: screenSize.screenWidth - controller.leftBarWidth,
      padding: const EdgeInsets.symmetric(horizontal: 2),
      child: EasyRefresh(
        controller: controller.refreshController,
        header: ClassicHeader(
          messageText: '正在刷新...',
          showText: true,
          processedText: '',
          readyText: '',
          dragText: '',
          armedText: '',
          clamping: false,
        ),
        footer: MaterialFooter(),
        onRefresh: () {
          logD('-----refresh------');
          controller.refreshController.finishRefresh();
          controller.refreshController.resetFooter();
        },
        onLoad: () {
          logD('-----onLoad------');
          controller.refreshController.finishLoad();
        },
        child: Obx(
          () => ListView(
            // physics: const ClampingScrollPhysics(),
            padding: EdgeInsets.all(4),
            children: [
              ClassifyDesc(
                name: controller.currentCategory.value?.name ?? '-',
                desc: controller.currentCategory.value?.desc,
              ),
              ...List.generate(
                goodsInfoMockData.length,
                (index) {
                  var goods = goodsInfoMockData[index];
                  return GoodsItemWidget(
                    goods: goods,
                    border: !(index >= goodsInfoMockData.length - 1),
                    activeDesc: goods.activity,
                    onPress: () => Get.toNamed(RouteNames.GoodsDetail.name,
                        parameters: Map.from({'id': goods.id.toString()})),
                    buyBtnClick: () => null,
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 左侧视图
  Widget _leftView() {
    return Obx(
      () => Container(
        width: controller.leftBarWidth,
        color: Color.fromARGB(255, 225, 221, 221),
        child: ListView(
          padding: const EdgeInsets.only(bottom: 40),
          children: categories.map((item) {
            var index = categories.indexOf(item);
            return InkWell(
              onTap: () {
                if (index != controller.selectedCategoryId) {
                  controller.onTapCategory(index);
                }
              },
              child: Container(
                alignment: Alignment.center,
                height: 44,
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: controller.selectedCategoryId == index
                          ? const Color.fromRGBO(242, 242, 242, 1)
                          : Colors.transparent,
                      width: 1,
                    ),
                  ),
                  color: const Color(0x00ffffff).withOpacity(
                      controller.selectedCategoryId == index ? 1.0 : 0.0),
                ),
                child: Text(
                  item.name,
                  style: TextStyle(
                    color: controller.selectedCategoryId == index
                        ? Color.fromRGBO(244, 18, 18, 1)
                        : Color.fromARGB(255, 123, 120, 120),
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}
